/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */
<script>

/**
 * 基本的echarts绘图组件
 */

import { on, off } from 'wind-dom';
import {throttle} from 'src/util/delay.ts';

export default {
    data () {
        return {
            screenWidth: document.body.clientWidth,
            screenHeight: document.body.clientHeight,
            oldHeight: 0,
            oldWidth: 0,
            lodashResize: {}
        };
    },

    mounted () {
        let vm = this;
        let resizeTime = 1000;

        vm.oldWidth = this.$el.clientWidth;
        vm.oldHeight = this.$el.clientHeight;
        vm.lodashResize = throttle(vm.resizeAction, resizeTime);
        on(window, 'resize', vm.lodashResize);

    },

    destroyed () {
        let vm = this;

        off(window, 'resize', vm.lodashResize);
    },
    watch: {
        screenWidth () {
            let vm = this;
            vm.screenResize();
        },
        screenHeight () {
            let vm = this;
            vm.screenResize();
        }
    },

    methods: {
        screenResize () {
            let vm = this;
            if (this.onResize) {
                if ((vm.$el.clientWidth > 0) && (vm.$el.clientHeight > 0) &&
                    (vm.oldWidth !== vm.$el.clientWidth || vm.oldHeight !== vm.$el.clientHeight)) {
                    vm.oldWidth = vm.$el.clientWidth;
                    vm.oldHeight = vm.$el.clientHeight;
                    vm.onResize();
                }
            }
        },
        resizeAction () {
            let vm = this;
            return (function () {
                window.screenWidth = document.getElementsByClassName('mainApp')[0].clientWidth;
                vm.screenWidth = window.screenWidth;
                window.screenHeight = document.getElementsByClassName('mainApp')[0].clientHeight;
                vm.screenHeight = window.screenHeight;
            })();
        },

        /* eslint-disable no-magic-numbers */

        countformat (count) {
            const MAX = 9999;
                const BIG_MAX = 9999999;

            if (count > MAX && count <= BIG_MAX) {
                count = Math.floor(count / 1000);
                count = count + 'k';
            }
            if (count > BIG_MAX) {
                count = MAX + 'k+';
            }
            return count;
        },

        /* eslint-enable no-magic-numbers */

        /* eslint-disable no-magic-numbers */
        /**
         *  饼图配色方案
         *  @param {number} [count] [饼块的数量，范围为1~12]
         *  @return {array} [返回的颜色数组]
         */
        colorMap: function (count) {
            switch (count) {
                case 0:
                case 1:
                case 2:
                    return ['#73F3E5', '#FFB93E'];
                case 3:
                case 4:
                    return ['#02EBAA', '#FFB93E', '#78B1FF', '#FF6B95'];

                case 5:

                    // return ['#02ebaa', '#ffec66', '#ffb93e', '#78b1ff', '#ff6b95'];
                    return ['#6E83E2', '#4FCAE5', '#2896CE', '#31C195', '#FFDB4B'];

                case 6:
                    return ['#02ebaa', '#ffec66', '#ffb93e', '#78b1ff', '#c994ff', '#ff6b95'];

                case 7:
                    return ['#02ebaa', '#ffec66', '#ffb93e', '#59bece', '#78b1ff', '#c994ff', '#ff6b95'];

                case 8:
                    return ['#02ebaa', '#ffec66', '#ffb93e', '#59bece', '#78b1ff', '#666dff', '#c994ff', '#ff6b95'];

                case 9:
                    return ['#02ebaa', '#83f37a', '#ffec66', '#ffb93d', '#59bece', '#78b1ff', '#666dff', '#c994ff', '#ff6b95'];

                case 10:
                    return ['#02ebaa', '#83f37a', '#b9ec4b', '#ffec66', '#ffb93d', '#59bece', '#78b1ff', '#666dff', '#c994ff', '#ff6b95'];

                case 11:
                    return ['#02ebaa', '#83f37a', '#b9ec4b', '#ffec66', '#ffb93d', '#59bece', '#78b1ff', '#666dff', '#c034e2', '#ff6b95', '#af614b'];

                case 12:
                    return ['#02ebaa', '#83f37a', '#b9ec4b', '#ffec66', '#ffb93d', '#59bece', '#78b1ff',
                        '#666dff', '#c994ff', '#ff6b95', '#ff9575', '#b9ab9b'];

                default:
                    return ['#02ebaa', '#83f37a', '#b9ec4b', '#ffec66', '#ffb93d', '#59bece', '#78b1ff',
                        '#666dff', '#c994ff', '#ff6b95', '#ff9575', '#b9ab9b'];
            }
        }

        /* eslint-enable no-magic-numbers */
    }
};
</script>
